import matplotlib.pyplot as plt
import numpy as np

# 修正后的动态权重公式（两种可选方案）
formula_option1 = r"$w_k = \frac{\exp(\beta \cdot \mathrm{sim}_k)}{\sum \exp(\beta \cdot \mathrm{sim}_k)} \cdot q_k^{1-\beta}$"
formula_option2 = r"$w_k = \frac{\exp(\beta \cdot sim_k)}{\sum \exp(\beta \cdot sim_k)} \cdot q_k^{1-\beta}$"

# 创建图形
fig, ax = plt.subplots(figsize=(5, 3))

# ===== 数据准备 =====
beta_values = np.linspace(0, 1, 100)
accuracy_mnist = 90.5 + 6.3 * np.exp(-5*(beta_values-0.8)**2)  # MNIST准确率曲线
accuracy_cifar = 72.0 + 4.2 * np.exp(-8*(beta_values-0.6)**2)  # CIFAR-10准确率曲线

# ===== 绘制曲线 =====
ax.plot(beta_values, accuracy_mnist, 'b-', linewidth=2.5, label='MNIST数据集')
ax.plot(beta_values, accuracy_cifar, 'r--', linewidth=2.5, label='CIFAR-10数据集')

# ===== 标记最优值 =====
ax.plot(0.8, 96.8, 'bo', markersize=10)
ax.plot(0.6, 76.2, 'ro', markersize=10)

# ===== 添加公式说明（使用修正后的公式）=====
# ax.text(0.5, 68, formula_option2, 
#         fontsize=14, ha='center', 
#         bbox=dict(facecolor='white', alpha=0.8, edgecolor='blue', boxstyle='round,pad=0.5'))

# ===== 图形美化 =====
ax.set_xlabel('融合系数 (β)', fontsize=12)
ax.set_ylabel('模型准确率 (%)', fontsize=12)
# ax.set_title('图12：融合系数β对模型准确率的影响', fontsize=14, fontweight='bold')
ax.grid(True, linestyle='--', alpha=0.3)
ax.legend(loc='lower center')
ax.set_ylim(65, 100)

# ===== 添加标注 =====
# ax.annotate('MNIST最优β=0.8\n准确率96.8%', xy=(0.8, 96.8), xytext=(0.65, 90),
#             arrowprops=dict(arrowstyle='->', color='blue'),
#             fontsize=10, bbox=dict(boxstyle="round,pad=0.3", fc="white", ec="blue", alpha=0.9))

# ax.annotate('CIFAR-10最优β=0.6\n准确率76.2%', xy=(0.6, 76.2), xytext=(0.35, 73),
#             arrowprops=dict(arrowstyle='->', color='red'),
#             fontsize=10, bbox=dict(boxstyle="round,pad=0.3", fc="white", ec="red", alpha=0.9))

plt.tight_layout()
plt.savefig('beta_impact_corrected.png', dpi=300)
plt.show()
